package com.ea.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ea.admin.mapper.AdminAccountMapper;
import com.ea.admin.mapper.AdminFunctionPermissionMapper;
import com.ea.admin.mapper.FunctionPermissionMapper;
import com.ea.admin.pojo.po.AdminAccount;
import com.ea.admin.pojo.po.AdminFunctionPermission;
import com.ea.admin.pojo.po.FunctionPermission;
import com.ea.admin.pojo.vo.account.FunctionPermissionVo;
import com.ea.admin.service.FunctionPermissionService;
import com.ea.common.result.BizException;
import com.ea.common.result.ErrorCode;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class FunctionPermissionServiceImpl implements FunctionPermissionService {

    @Resource
    FunctionPermissionMapper functionPermissionMapper;
    @Resource
    AdminFunctionPermissionMapper adminFunctionPermissionMapper;
    @Resource
    AdminAccountMapper adminAccountMapper;

    @Override
    public List<FunctionPermissionVo> getFunctionPermissionByAccountId(Long accountId) {
        List<FunctionPermission> functionPermissionList = adminFunctionPermissionMapper.
                getFunctionPermissionListByAccountId(accountId);
        return parseFunctionPermissionToVo(functionPermissionList);
    }

    @Override
    public List<Integer> getDefaultFunctionPermissionCode(Long adminRoleId) {
        return functionPermissionMapper
                .getDefaultPermissionByAdminRoleId(adminRoleId).stream().map(FunctionPermission::getPermissionCode)
                .collect(Collectors.toList());
    }

    @Override
    public List<FunctionPermissionVo> getDefaultFunctionPermission(Long adminRoleId) {
        List<FunctionPermission> list = functionPermissionMapper
                .getDefaultPermissionByAdminRoleId(adminRoleId);
        return parseFunctionPermissionToVo(list);
    }


    @Override
    public List<Integer> getAllFunctionPermission() {
        return functionPermissionMapper.selectList(null).stream()
                .map(FunctionPermission::getPermissionCode)
                .collect(Collectors.toList());
    }

    @Override
    public void addFunctionPermission(Long accountId, List<Integer> functionPermissionList) {
        if (functionPermissionList == null) return;
        // 添加权限
        List<AdminFunctionPermission> adminFunctionPermissionList = new ArrayList<>();
        List<FunctionPermission> requestFunctionPermissionList = functionPermissionMapper.selectBatchIds(functionPermissionList);
        AdminAccount adminAccount = adminAccountMapper.selectById(accountId);
        if (adminAccount == null) {
            throw new BizException(ErrorCode.SYSTEM_RESOURCE_ADMIN_ACCOUNT_NOT_EXISTS);
        }
        requestFunctionPermissionList.forEach(
                request -> {
                    // 如果是超级管理员权限，则不允许分配给普通管理员
                    // 不为1，即不为超级管理员
                    if (adminAccount.getAdminRoleId() != 1
                            && request.getIsSuperAdminPermission() == 1) {
                        throw new BizException(ErrorCode.USER_FUNCTION_PERMISSION_ERROR_ONLY_SUPER_ADMIN);
                    }
                    adminFunctionPermissionList.add(
                            new AdminFunctionPermission(
                                    null,
                                    request.getPermissionCode(),
                                    accountId,
                                    0
                            )
                    );
                }
        );

        if (!adminFunctionPermissionList.isEmpty()) {
            adminFunctionPermissionMapper.insertBatchSomeColumn(adminFunctionPermissionList);
        }
    }

    @Override
    public void deleteFunctionPermissionByAccountId(Long accountId) {
        LambdaQueryWrapper<AdminFunctionPermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdminFunctionPermission::getAccountId, accountId);
        adminFunctionPermissionMapper.delete(wrapper);
    }

    private List<FunctionPermissionVo> parseFunctionPermissionToVo(List<FunctionPermission> functionPermissionList) {
        List<FunctionPermissionVo> voList = new ArrayList<>();
        Map<Integer, FunctionPermissionVo> map = new HashMap<>();
        functionPermissionList.forEach(functionPermission -> {
            // 如果是根权限，直接添加到list中
            if (functionPermission.getSupPermissionCode() == null) {
                FunctionPermissionVo functionPermissionVo = new FunctionPermissionVo();
                functionPermissionVo.setPermissionCode(functionPermission.getPermissionCode());
                functionPermissionVo.setPermissionName(functionPermission.getPermissionName());
                functionPermissionVo.setSubPermissionList(new ArrayList<>());
                voList.add(functionPermissionVo);
                map.put(functionPermission.getPermissionCode(), functionPermissionVo);
            }
            // 不是父权限，则从map中取出，并添加到sub的列表中
            else {
                FunctionPermissionVo getParentVo = map.get(functionPermission.getSupPermissionCode());
                if (getParentVo != null) {
                    getParentVo.getSubPermissionList().add(
                            new FunctionPermissionVo(
                                    functionPermission.getPermissionCode(),
                                    functionPermission.getPermissionName(),
                                    null
                            )
                    );
                }
            }
        });
        return voList;
    }
}
